package com.Offer;

import com.pojo.TreeNode;

/*
    面试题55：二叉树的深度
    输入一颗二叉树的根节点，求该树的深度
        5
       / \
      3   7
     / \ / \
    2  4 6  8
   /
  1

 */
public class demo55 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        int[] data = {3,7,2,4,6,8,1};
        for (int datum : data) {
            TreeNode.add(datum,root);
        }
        System.out.println(TreeNode.depth(root));
        System.out.println(isBalanced(root));
    }

    //举一反三：平衡二叉树,输入一个二叉树的根节点，判断这个树是否是平衡二叉树
    public static boolean isBalanced(TreeNode root){
        if (root == null)
            return true;
        int left = TreeNode.depth(root.left);
        int right = TreeNode.depth(root.right);
        int diff = left-right;
        if(diff > 1 || diff < -1)
            return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }
}
